package com.duobaoyu.dby.supply.admin.service.impl;

import com.duobaoyu.dby.channel.adapter.enums.ChannelCodeEnum;
import com.duobaoyu.dby.channel.common.util.SupplyCollectionUtils;
import com.duobaoyu.dby.common.mybatis.util.EnumUtils;
import com.duobaoyu.dby.supply.admin.service.AdminChannelToolBizService;
import com.duobaoyu.dby.supply.biz.client.boundary.channel.SupplyChainChannelToolClient;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderBO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderProductBO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderQueryInBO;
import com.duobaoyu.dby.supply.biz.service.order.OrderService;
import com.duobaoyu.dby.supply.biz.service.refund.AfterSaleService;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleOutBO;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleQuery;
import com.duobaoyu.dby.supply.common.biz.dto.tool.ChannelToolAfterSaleAttributeDTO;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chenms
 * @date 2023/11/27 9:15 AM
 */
@Service
@RequiredArgsConstructor
public class AdminChannelToolBizServiceImpl implements AdminChannelToolBizService {

    private final OrderService orderService;
    private final AfterSaleService afterSaleService;
    private final SupplyChainChannelToolClient supplyChainChannelToolClient;

    @Override
    public Object getChannelOrderInfo(String orderSn) {
        List<OrderBO> orderList = orderService.list(OrderQueryInBO.builder().orderSnList(Lists.newArrayList(orderSn)).returnProduct(Boolean.TRUE).build());
        OrderBO order = SupplyCollectionUtils.getSingleDataFirst(orderList);
        CustomAssert.notNull(order, "订单不存在");
        return supplyChainChannelToolClient.getChannelOrderInfoResponse(order);
    }

    @Override
    public Object getChannelOutboundOrder(String orderSn) {
        List<OrderBO> orderList = orderService.list(OrderQueryInBO.builder().orderSnList(Lists.newArrayList(orderSn)).build());
        OrderBO order = SupplyCollectionUtils.getSingleDataFirst(orderList);
        CustomAssert.notNull(order, "订单不存在");
        return supplyChainChannelToolClient.getChannelOutboundOrderResponse(order);
    }

    @Override
    public Object getChannelAfterSaleDetail(String afterSaleSn) {
        AfterSaleOutBO afterSaleOut = afterSaleService.queryAfterSale(AfterSaleQuery.builder().afterSaleSn(afterSaleSn).build());
        CustomAssert.notNull(afterSaleOut, "售后单不存在");
        return supplyChainChannelToolClient.getChannelAfterSaleDetailResponse(afterSaleOut);
    }

    @Override
    public Object getChannelAfterSaleAttributes(ChannelToolAfterSaleAttributeDTO channelToolAfterSaleAttribute) {
        List<OrderBO> orderList = orderService.list(OrderQueryInBO.builder().orderSnList(Lists.newArrayList(channelToolAfterSaleAttribute.getOrderSn()))
                .skuCodeList(channelToolAfterSaleAttribute.getSkuCodeList()).returnProduct(Boolean.TRUE).build());
        OrderBO order = SupplyCollectionUtils.getSingleDataFirst(orderList);
        CustomAssert.notNull(order, "订单不存在");
        if (CollectionUtils.isNotEmpty(channelToolAfterSaleAttribute.getSkuCodeList())) {
            List<OrderProductBO> orderProductList = order.getOrderProductList();
            CustomAssert.notEmpty(orderProductList, "订单商品不存在");
            Set<String> existsSkuCode = orderProductList.stream().map(OrderProductBO::getSkuCode).collect(Collectors.toSet());
            Collection<String> subtract = CollectionUtils.subtract(channelToolAfterSaleAttribute.getSkuCodeList(), existsSkuCode);
            CustomAssert.isTrue(CollectionUtils.isEmpty(subtract), String.format("部分商品不存在:%s", subtract));
        }
        if (EnumUtils.eq(ChannelCodeEnum.LINKED_MALL, order.getChannelCode())) {
            CustomAssert.notNull(channelToolAfterSaleAttribute.getAfterSaleType(), "售后类型不能为空");
            CustomAssert.notNull(channelToolAfterSaleAttribute.getGoodsStatus(), "货物状态不能为空");
        } else if (EnumUtils.eq(ChannelCodeEnum.ALIBABA_DISTRIBUTION, order.getChannelCode())) {
            CustomAssert.notNull(channelToolAfterSaleAttribute.getGoodsStatus(), "货物状态不能为空");
        }
        return supplyChainChannelToolClient.getChannelAfterSaleAttributesResponse(order, channelToolAfterSaleAttribute.getAfterSaleType(), channelToolAfterSaleAttribute.getGoodsStatus());
    }

    @Override
    public Object getChannelProductSkuDetail(String skuCode) {
        return supplyChainChannelToolClient.getChannelProductSkuDetailResponse(skuCode);
    }

}
